package com.edu.web.controller.info;

import com.edu.common.annotation.Log;
import com.edu.common.core.controller.BaseController;
import com.edu.common.core.domain.AjaxResult;
import com.edu.common.core.page.TableDataInfo;
import com.edu.common.enums.BusinessType;
import com.edu.common.exception.ServiceException;
import com.edu.common.utils.poi.ExcelUtil;
import com.edu.framework.websocket.service.CardService;
import com.edu.system.domain.CardUser;
import com.edu.system.domain.HandRechargeReq;
import com.edu.system.domain.TCard;
import com.edu.system.service.CardUserService;
import com.edu.system.service.ITCardService;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 卡用户管理
 *
 * @author xxxx
 * @date 2024-01-18
 */
@RestController
@RequestMapping("/system/cardUser")
public class CardUserController extends BaseController {
    @Autowired
    private CardUserService cardUserService;
    @Resource
    private CardService wsCardService;
    @Resource
    private ITCardService itCardService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:cardUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(CardUser user) {
        startPage();
        List<CardUser> list = cardUserService.selectList(user);
        return getDataTable(list);
    }

    /**
     * 获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cardUserService.selectByCardId(id));
    }

    /**
     * 导出
     */
    @PreAuthorize("@ss.hasPermi('system:cardUser:export')")
    @Log(title = "用户余额", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CardUser user) {
        List<CardUser> list = cardUserService.selectList(user);
        ExcelUtil<CardUser> util = new ExcelUtil<CardUser>(CardUser.class);
        if (CollectionUtils.isEmpty(list)){
            throw new ServiceException("没有数据导出！");
        }
        if (!user.getIsSplitClass()){
            util.exportExcel(response, list, "用户余额");
        } else {
            Map<String, List<CardUser>> collect = list.stream()
                .collect(Collectors.groupingBy(CardUser::getClassName));
            util.exportExcel(response, collect);
        }

    }

    @Log(title = "批量充值", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:cardUser:batchRecharge')")
    @PostMapping("/batchRecharge")
    public AjaxResult batchRecharge(MultipartFile file) throws Exception {
        ExcelUtil<CardUser> util = new ExcelUtil<CardUser>(CardUser.class);
        List<CardUser> list = util.importAllSheet(file.getInputStream());
        String message = cardUserService.rechargeByList(list);

        // 批量充值就全量推
        CompletableFuture.runAsync(() -> wsCardService.pushCardList(new ArrayList<>()));
        return success(message);
    }

    /**
     * 下载批量充值模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<CardUser> util = new ExcelUtil<CardUser>(CardUser.class);
        util.importTemplateExcel(response, "班级");
    }

    /**
     * 手动充值
     */
    @PreAuthorize("@ss.hasPermi('system:cardUser:recharge')")
    @Log(title = "手动充值", businessType = BusinessType.IMPORT)
    @PostMapping("/recharge")
    public AjaxResult recharge(@RequestBody HandRechargeReq req) {
        String recharge = cardUserService.recharge(req.getCardIds(), req.getBalance());
        wsCardService.pushCardList(req.getCardIds());
        return success(recharge);
    }

    @GetMapping(value = "/cnt")
    public AjaxResult getCnt() {
        return success(cardUserService.getCardCnt());
    }


}
